package com.webex.ta.hydra.gui.table.cellrenderer;

import com.webex.ta.hydra.core.Command;

import javax.swing.*;
import javax.swing.plaf.UIResource;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-18
 * Time: 15:20:32
 */
public class BreakPointRenderer extends JComponent implements TableCellRenderer, UIResource {

    private static Image image;

    private boolean enabled;
    private Color bg;

    public BreakPointRenderer() {
        if (image == null) {
            image = getToolkit().getImage(
                    getClass().getResource("/com/webex/ta/hydra/images/breakpoint.png"));
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(image, 0);

            try {
                tracker.waitForAll();
            } catch (Exception e) {
            }
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        enabled = (value != null && ((Command) value).isBreakPoint());
        bg = isSelected ? table.getSelectionBackground() : null;
        return this;
    }

    public void setDebugEnabled(boolean value) {
        enabled = value;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Dimension size = getSize();
        if (bg != null) {
            g.setColor(bg);
            g.fillRect(0, 0, size.width, size.height);
        }
        if (enabled)
            g.drawImage(image, (size.width - image.getWidth(null)) / 2, 3, null);
    }
}
